#!/bin/sh

# Checking cross compile
hostos=`uname -s | sed -e 's/\-.*//'`
hostarch=`uname -m | sed -e 's/i.86/x86/'`
if [ "$hostos" = "AIX" ] || [ "$hostos" = "SunOS" ]; then
    hostarch=`uname -p`
fi
case "$hostarch" in
    amd64) hostarch=x86_64 ;;
    arm*) [ "$hostarch" = "arm64" ] || hostarch='arm' ;;
    aarch64) hostarch=arm64 ;;
    powerpc*|ppc*) hostarch=power ;;
    s390x) hostarch=zarch ;;
esac

makefile="$1"
config="$2"

compiler_name="$3"
shift 3
flags="$*"

# First, we need to know the target OS and compiler name
{
    data=`$compiler_name $flags -E ctest.c`
} || {
    printf '%s\n' "C Compiler ($compiler_name) is something wrong." >&2
    exit 1
}

cross_suffix=""

if [ "`dirname "$compiler_name"`" != '.' ]; then
    cross_suffix="$cross_suffix`dirname "$compiler_name"`/"
fi

cn=`echo $compiler_name | sed -e 's/ -.*//'`
bn=`basename "$cn"`

case "$bn" in
    *-*) if [ "$bn" != '-' ]; then
           cross_suffix="$cross_suffix${bn%-*}-"
         fi
esac

compiler=""
case "$data" in
    *COMPILER_LSB*) compiler=LSB ;;
    *COMPILER_CLANG*) compiler=CLANG ;;
    *COMPILER_PGI*) compiler=PGI ;;
    *COMPILER_PATHSCALE*) compiler=PATHSCALE ;;
    *COMPILER_INTEL*) compiler=INTEL ;;
    *COMPILER_OPEN64*) compiler=OPEN64 ;;
    *COMPILER_SUN*) compiler=SUN ;;
    *COMPILER_IBM*) compiler=IBM ;;
    *COMPILER_DEC*) compiler=DEC ;;
    *COMPILER_FUJITSU*) compiler=FUJITSU ;;
esac
if [ -z "$compiler" ]; then
    compiler=GCC
fi

case "$data" in *OS_LINUX*) os=Linux ;; esac
case "$data" in *OS_FREEBSD*) os=FreeBSD ;; esac
case "$data" in *OS_NETBSD*) os=NetBSD ;; esac
case "$data" in *OS_OPENBSD*) os=OpenBSD ;; esac
case "$data" in *OS_DRAGONFLY*) os=DragonFly ;; esac
case "$data" in *OS_DARWIN*) os=Darwin ;; esac
case "$data" in *OS_SUNOS*) os=SunOS ;; esac
case "$data" in *OS_AIX*) os=AIX ;; esac
case "$data" in *OS_OSF*) os=osf ;; esac
case "$data" in *OS_WINNT*) os=WINNT ;; esac
case "$data" in *OS_CYGWIN_NT*) os=CYGWIN_NT ;; esac
case "$data" in *OS_INTERIX*) os=Interix ;; esac
case "$data" in *OS_ANDROID*) os=Android ;; esac
case "$data" in *OS_HAIKU*) os=Haiku ;; esac

case "$data" in
    *ARCH_X86_64*) architecture=x86_64 ;;
    *ARCH_X86*) architecture=x86 ;;
    *ARCH_E2K*) architecture=e2k ;;
    *ARCH_POWER*) architecture=power ;;
    *ARCH_MIPS64*) architecture=mips64 ;;
    *ARCH_MIPS*) architecture=mips ;;
    *ARCH_ALPHA*) architecture=alpha ;;
    *ARCH_SPARC*) architecture=sparc ;;
    *ARCH_IA64*) architecture=ia64 ;;
    *ARCH_ARM64*) architecture=arm64 ;;
    *ARCH_ARM*) architecture=arm ;;
    *ARCH_ZARCH*) architecture=zarch ;;
    *ARCH_RISCV64*) architecture=riscv64 ;;
    *ARCH_LOONGARCH64*) architecture=loongarch64 ;;
    *ARCH_CSKY*) architecture=csky ;;
esac

defined=0

if [ "$os" = "AIX" ]; then
    if [ "$compiler" = "GCC" ]; then
        case "$BINARY" in
            32) compiler_name="$compiler_name -maix32" ;;
            64) compiler_name="$compiler_name -maix64" ;;
        esac
        defined=1
    else
        case "$BINARY" in
            32) compiler_name="$compiler_name -m32" ;;
            64) compiler_name="$compiler_name -m64" ;;
        esac
        defined=1
    fi
fi

case "$architecture" in
    mips)
    	compiler_name="$compiler_name -mabi=32"
    	defined=1
    	;;
    mips64)
    	case "$BINARY" in
    	    32) compiler_name="$compiler_name -mabi=n32" ;;
    	    64) compiler_name="$compiler_name -mabi=64" ;;
    	esac
    	defined=1
    	;;
    arm|arm64) defined=1 ;;
    zarch|e2k|alpha|ia64|riscv64|loonarch64)
    	defined=1
    	BINARY=64
    	;;
    x86)
    	[ "$os" != "Darwin" ] && [ "$os" != "SunOS" ] && {
    	    defined=1
    	    BINARY=32
    	}
    	;;
esac

case "$compiler" in
    PGI)
    	case "$BINARY" in
    	    32) compiler_name="$compiler_name -tp p7" ;;
    	    64) compiler_name="$compiler_name -tp p7-64" ;;
    	esac
    	openmp='-mp'
    	defined=1
    	;;
    IBM)
    	case "$BINARY" in
    	    32) compiler_name="$compiler_name -q32" ;;
    	    64) compiler_name="$compiler_name -q64" ;;
    	esac
    	openmp='-qsmp=omp'
    	defined=1
    	;;
    INTEL) openmp='-openmp' ;;
    PATHSCALE|OPEN64) openmp='-mp' ;;
    CLANG|GCC|LSB) openmp='-fopenmp' ;;
    FUJITSU) openmp='-Kopenmp' ;;
esac

if [ "$defined" -eq 0 ]; then
    case "$BINARY" in
    	32) compiler_name="$compiler_name -m32" ;;
    	64) compiler_name="$compiler_name -m64" ;;
    esac
fi

# Do again
{
    data="$($compiler_name $flags -E ctest.c)"
} || {
    printf '%s\n' "C Compiler ($compiler_name) is something wrong." >&2
    exit 1
}

no_msa=0
if [ "$architecture" = "mips" ] || [ "$architecture" = "mips64" ]; then
    tmpd=$(mktemp -d 2>/dev/null || mktemp -d -t 'OBC')
    tmpf="$tmpd/a.c"
    code='"addvi.b $w0, $w1, 1"'
    msa_flags='-mmsa -mfp64 -mload-store-pairs'
    printf "#include <msa.h>\n\n" >> "$tmpf"
    printf "void main(void){ __asm__ volatile(%s); }\n" "$code" >> "$tmpf"

    args="$msa_flags -o $tmpf.o $tmpf"
    {
    	$compiler_name $flags $args >/dev/null 2>&1
    } || {
    	no_msa=1
    }

    rm -rf "$tmpd"
fi

no_lsx=0
no_lasx=0
if [ "$architecture" = "loongarch64" ]; then
    tmpd="$(mktemp -d)"
    tmplsx="$tmpd/lsx.c"
    codelsx='"vadd.b $vr0, $vr0, $vr0"'
    lsx_flags='-march=loongarch64'
    printf "void main(void){ __asm__ volatile(%s);}\n" "$codelsx" >> "$tmplsx"
    args="$lsx_flags -o $tmplsx.o $tmplsx"
    {
        $compiler_name $flags $args >/dev/null 2>&1
    } || {
        no_lsx=1
    }

    tmplasx="$tmpd/lasx.c"
    codelasx='"xvadd.b $xr0, $xr0, $xr0"'
    lasx_flags='-march=loongarch64'
    printf "void main(void){ __asm__ volatile(%s);}\n" "$codelasx" >> "$tmplasx"
    args="$lasx_flags -o $tmplasx.o $tmplasx"
    {
        $compiler_name $flags $args >/dev/null 2>&1
    } || {
        no_lasx=1
    }

    rm -rf "$tmpd"
fi

case "$data" in
    *ARCH_X86_64*) architecture=x86_64 ;;
    *ARCH_X86*) architecture=x86 ;;
    *ARCH_E2K*) architecture=e2k ;;
    *ARCH_POWER*) architecture=power ;;
    *ARCH_MIPS64*) architecture=mips64 ;;
    *ARCH_MIPS*) architecture=mips ;;
    *ARCH_ALPHA*) architecture=alpha ;;
    *ARCH_SPARC*) architecture=sparc ;;
    *ARCH_IA64*) architecture=ia64 ;;
    *ARCH_ARM64*) architecture=arm64 ;;
    *ARCH_ARM*) architecture=arm ;;
    *ARCH_ZARCH*) architecture=zarch ;;
    *ARCH_LOONGARCH64*) architecture=loongarch64 ;;
    *ARCH_CSKY*) architecture=csky ;;
esac

binformat='bin32'
case "$data" in
    *BINARY_64*) binformat='bin64' ;;
esac

no_avx512=0
no_avx512bf=0
if [ "$architecture" = "x86" ] || [ "$architecture" = "x86_64" ]; then
    tmpd=$(mktemp -d 2>/dev/null || mktemp -d -t 'OBC')
    tmpf="$tmpd/a.c"
    code='"vbroadcastss -4 * 4(%rsi), %zmm2"'
    printf "#include <immintrin.h>\n\nint main(void){ __asm__ volatile(%s); }\n" "$code" >> "$tmpf"
    if [ "$compiler" = "PGI" ]; then
        args=" -tp skylake -c -o $tmpf.o $tmpf"
    else
        args=" -march=skylake-avx512 -c -o $tmpf.o $tmpf"
    fi
    no_avx512=0
    {
    	$compiler_name $flags $args >/dev/null 2>&1
    } || {
    	no_avx512=1
    }

    rm -rf "$tmpd"
    if [ "$no_avx512" -eq 0 ]; then
    tmpd=$(mktemp -d 2>/dev/null || mktemp -d -t 'OBC')
    tmpf="$tmpd/a.c"
    code='"__m512 a= _mm512_dpbf16_ps(a, (__m512bh) _mm512_loadu_si512(%1]), (__m512bh) _mm512_loadu_si512(%2]));"'
    printf "#include <immintrin.h>\n\nint main(void){ %s; }\n" "$code" >> "$tmpf"
    if [ "$compiler" = "PGI" ]; then
        args=" -tp cooperlake -c -o $tmpf.o $tmpf"
    else
        args=" -march=cooperlake -c -o $tmpf.o $tmpf"
    fi
    no_avx512bf=0
    {
        $compiler_name $flags $args >/dev/null 2>&1
    } || {
        no_avx512bf=1
    }

    rm -rf "$tmpd"
  fi
fi

no_rv64gv=0
if [ "$architecture" = "riscv64" ]; then
    tmpd=$(mktemp -d 2>/dev/null || mktemp -d -t 'OBC')
    tmpf="$tmpd/a.c"
    code='"vsetvli    zero, zero, e8, m1\n"'
    printf "int main(void){ __asm__ volatile(%s); }\n" "$code" >> "$tmpf"
    args=" -march=rv64gv -c -o $tmpf.o $tmpf"
    no_rv64gv=0
    {
        $compiler_name $flags $args >/dev/null 2>&1
    } || {
        no_rv64gv=1
    }
    rm -rf "$tmpd"
fi

no_sve=0
if [ "$architecture" = "arm64" ]; then
    tmpd=$(mktemp -d 2>/dev/null || mktemp -d -t 'OBC')
    tmpf="$tmpd/a.c"
    printf "#include <arm_sve.h>\n\n int main(void){}\n">> "$tmpf"
    args=" -march=armv8-a+sve -c -o $tmpf.o $tmpf"
    no_sve=0
    {
        $compiler_name $flags $args >/dev/null 2>&1
    } || {
        args=" -Msve_intrinsics -c -o $tmpf.o $tmpf"
        $compiler_name $flags $args >/dev/null 2>&1
    } || {
        no_sve=1
    }
    rm -rf "$tmpd"
fi

c11_atomics=0
case "$data" in
    *HAVE_C11*)
        tmpd=$(mktemp -d 2>/dev/null || mktemp -d -t 'OBC')
        tmpf="$tmpd/a.c"
        printf "#include <stdatomic.h>\nint main(void){}\n" >> "$tmpf"
        args=" -c -o $tmpf.o $tmpf"
        c11_atomics=1
        {
            $compiler_name $flags $args >/dev/null 2>&1
        } || {
            c11_atomics=0
        }

        rm -rf "$tmpd"
        ;;
esac

oldgcc=0
no_avx2=0
if [ "$compiler" = "GCC" ]; then
    case "$architecture" in x86|x86_64)
        no_avx2=0
        oldgcc=0
        data=`$compiler_name -dumpversion`
        case "$data" in *.*.*)
            data="${data%.*}"
        esac
        if awk -v n1=$data -v n2=4.6 'BEGIN { exit !(n1 <= n2) }'; then
            no_avx2=1
            oldgcc=1
        fi
    esac
fi

data=`$compiler_name $flags -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`

need_fu=''
if echo "$data" | grep 'globl[[:space:]][_\.]'; then
    need_fu="${data##*globl[[:space:]]}"
    need_fu="${need_fu%%[!_\.]*}"
fi

cross=0

if [ "$architecture" != "$hostarch" ]; then
    cross=1
    [ "$hostarch" = "x86_64" ] && [ "$architecture" = "x86" ] && cross=0
    [ "$hostarch" = "mips64" ] && [ "$architecture" = "mips" ] && cross=0
fi

[ "$os" != "$hostos" ] && cross=1
[ "$os" = "Android" ] && [ "$hostos" = "Linux" ] && [ -n "$TERMUX_APP_PID" ] \
    && cross=0

[ "$USE_OPENMP" != 1 ] && openmp=''

linker_L=""
linker_l=""
linker_a=""

link=`$compiler_name $flags -c ctest2.c -o ctest2.o 2>&1 && $compiler_name $flags $openmp -v ctest2.o -o ctest2 2>&1 && rm -f ctest2.o ctest2 ctest2.exe`

link=`echo "$link" | sed 's/\-Y[[:space:]]P\,/\-Y/g'`


flags=`echo $link | tr "'[[:space:]],\n" " "`

# Strip trailing quotes
old_flags="$flags"
flags=''


for flag in $old_flags; do
    f=`echo "$flag" | tr '"' ' '`
    flags="$flags $f"
done

for flag in $flags; do
    case "$flag" in -L*)
        case "$flag" in
            -LIST:*|-LANG:*) ;;
            *) linker_L="$linker_L $flag" ;;
        esac
    esac

    case "$flag" in -Y*)
        linker_L="$linker_L -Wl,$flag" ;;
    esac

    case "$flag" in --exclude-libs*)
        linker_L="$linker_L -Wl,$flag"
        flags=""
        ;;
    esac

    case "$flag" in -l*)
        case "$flag" in
            *gfortranbegin*|*frtbegin*|*pathfstart*|*numa*|*crt[0-9]*|\
                *gcc*|*user32*|*kernel32*|*advapi32*|*shell32*|*omp*|\
                *[0-9]*) ;;
            *) linker_l="$linker_l $flag" ;;
        esac
    esac

    case "$flag" in *.a) linker_a="$linker_a $flag" ;; esac
done

 [ "$makefile" = "-" ] && {
    [ "$no_rv64gv" -eq 1 ] && printf "NO_RV64GV=1\n"
    [ "$no_avx512" -eq 1 ] && printf "NO_AVX512=1\n"
    [ "$no_avx512bf" -eq 1 ] && printf "NO_AVX512BF16=1\n"
    [ "$no_avx2" -eq 1 ] && printf "NO_AVX2=1\n"
    [ "$oldgcc" -eq 1 ] && printf "OLDGCC=1\n"
    exit 0
}

:> "$makefile" || exit 1
:> "$config" || exit 1


# print $data, "\n";

{
    printf "OSNAME=%s\n" "$os"
    printf "ARCH=%s\n" "$architecture"
    printf "C_COMPILER=%s\n" "$compiler"
    [ $binformat != 'bin32' ] && printf "BINARY32=\n"
    [ $binformat != 'bin64' ] && printf "BINARY64=\n"
    [ "$binformat" = "bin32" ] && printf "BINARY32=1\n"
    [ "$binformat" = "bin64" ] && printf "BINARY64=1\n"
    [ -n "$need_fu" ] && printf 'FU=%s\n' "$need_fu"
    [ "$cross" -ne 0 ] && [ -n "$cross_suffix" ] && \
        printf "CROSS_SUFFIX=%s\n" "$cross_suffix"
    [ "$cross" -ne 0 ] && printf "CROSS=1\n"
    printf "CEXTRALIB=%s %s %s\n" "$linker_L" "$linker_l" "$linker_a"
    [ "$no_msa" -eq 1 ] &&  printf "NO_MSA=1\n"
    [ "$no_sve" -eq 1 ] && printf "NO_SVE=1\n"
    [ "$no_rv64gv" -eq 1 ] && printf "NO_RV64GV=1\n"
    [ "$no_avx512" -eq 1 ] && printf "NO_AVX512=1\n"
    [ "$no_avx512bf" -eq 1 ] && printf "NO_AVX512BF16=1\n"
    [ "$no_avx2" -eq 1 ] && printf "NO_AVX2=1\n"
    [ "$oldgcc" -eq 1 ] && printf "OLDGCC=1\n"
    [ "$no_lsx" -eq 1 ] && printf "NO_LSX=1\n"
    [ "$no_lasx" -eq 1 ] && printf "NO_LASX=1\n"
} >> "$makefile"

os=`echo "$os" | tr '[[:lower:]]' '[[:upper:]]'/ `
architecture=`echo "$architecture" | tr '[[:lower:]]' '[[:upper:]]' `
compiler=`echo "$compiler" | tr '[[:lower:]]' '[[:upper:]]' `

{
    printf "#define OS_%s\t1\n" "$os"
    printf "#define ARCH_%s\t1\n" "$architecture"
    printf "#define C_%s\t1\n" "$compiler"
    [ "$binformat" = "bin32" ] && printf "#define __32BIT__\t1\n"
    [ "$binformat" = "bin64" ] && printf "#define __64BIT__\t1\n"
    [ -n "$need_fu" ] && printf "#define FUNDERSCORE\t%s\n" "$need_fu"
    [ "$no_msa" -eq 1 ] && printf "#define NO_MSA\t1\n"
    [ "$c11_atomics" -eq 1 ] && printf "#define HAVE_C11\t1\n"
    [ "$no_lsx" -eq 1 ] && printf "#define NO_LSX\t1\n"
    [ "$no_lasx" -eq 1 ] && printf "#define NO_LASX\t1\n"
} >> "$config"


if [ "$os" = "LINUX" ]; then

#    @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);

#    if ($pthread[2] ne "") {
#	print CONFFILE "#define PTHREAD_CREATE_FUNC	$pthread[2]\n";
#    } else {
	printf "#define PTHREAD_CREATE_FUNC	pthread_create\n" >> "$config"
#    }
else
    printf "#define PTHREAD_CREATE_FUNC	pthread_create\n" >> "$config"
fi
